﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using ChinapnrAPI;

namespace Uranus.ChinaService.Receives
{
    public class BaseReceive : BaseChinapnr
    {
        /// <summary>
        /// 存管回调请求上下文
        /// </summary>
        protected HttpContext HttpContext { get; set; }

        /// <summary>
        /// 存管回调请求
        /// </summary>
        protected HttpRequest Request { get; set; }

        /// <summary>
        /// 存管回调请求
        /// </summary>
        protected HttpResponse Response { get; set; }

        /// <summary>
        /// 当前请求流水号
        /// </summary>
        protected string CurrentSeqCode { get; set; }

        /// <summary>
        /// 接受的参数合集
        /// </summary>
        protected NameValueCollection NameValueParams { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContext"></param>
        public BaseReceive(HttpContext httpContext)
        {
            if (httpContext == null || httpContext.Request == null)
            {
                throw new ArgumentNullException("httpContext", "请求上下文未null");
            }

            this.HttpContext = httpContext;
            this.Request = httpContext.Request;
            this.Response = httpContext.Response;

            this.CmdId = this.Request["CmdId"];
            this.RespType = this.Request["RespType"];
            if (string.IsNullOrEmpty(this.CmdId) && string.IsNullOrEmpty(RespType))
            {
                throw new ArgumentNullException("httpContext", "没有CmdId参数");
            }
            //设置响应数据
            base.SetRequest(this.Request);
            this.MerPriv = Common.CodeCommon.UrlUtilityDeCode(this.MerPriv);
            //组装参数集合
            this.NameValueParams = new NameValueCollection();
            foreach (string key in this.Request.Form.Keys)
            {
                NameValueParams.Add(key, this.Request.Form[key]);
            }
            this.FreezeTrxId = this.Request["FreezeTrxId"];
            this.CurrentSeqCode = Guid.NewGuid().ToString("N");
            this.WriteStartLog();

        }

        private void WriteStartLog()
        {
            string title = string.Format("接收回调请求{0}", this.CmdId);
            StringBuilder builder = new StringBuilder();
            builder.Append("回调请求参数\r\n");
            builder.Append(String.Join("\r\n", NameValueParams.AllKeys.Select(p => string.Format("{0}：{1}", p, NameValueParams[p]))));

            Dictionary<string, string> dicObj = new Dictionary<string, string>();
            dicObj.Add("CmdId", this.CmdId);
            dicObj.Add("CurrentSeqCode", this.CurrentSeqCode);
            dicObj.Add("CurrentTime", DateTime.Now.ToString());

            ChinaServiceCommon.WriteInfoLog(title, builder.ToString(), dicObj, this.Request);
        }

        /// <summary>
        /// 写入操作详情日志
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="writeLogParams"></param>
        protected void WriteInfoLog(string title, string content, Dictionary<string, string> writeLogParams)
        {
            Dictionary<string, string> writeLogObject = writeLogParams ?? new Dictionary<string, string>();
            this.SetWriteLogObject(writeLogObject);
            ChinaServiceCommon.WriteInfoLog(title, content, writeLogObject, this.Request);
        }

        /// <summary>
        /// 写入操作详情日志
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        protected void WriteInfoLog(string title, string content)
        {
            this.WriteInfoLog(title, content, null);
        }

        /// <summary>
        /// 写入错误日志
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="writeLogParams"></param>
        protected void WriteErrorLog(string title, string content, Dictionary<string, string> writeLogParams, bool isSendEmail)
        {
            Dictionary<string, string> writeLogObject = writeLogParams ?? new Dictionary<string, string>();
            this.SetWriteLogObject(writeLogObject);
            ChinaServiceCommon.WriteErrorLog(title, content, writeLogObject, this.Request, isSendEmail);
        }

        /// <summary>
        /// 写入错误日志
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        protected void WriteErrorLog(string title, string content)
        {
            this.WriteErrorLog(title, content, null, false);
        }

        ///// <summary>
        ///// 写入错误日志内容为参数
        ///// </summary>
        ///// <param name="title"></param>
        ///// <param name="content"></param>
        //protected void WriteParamErrorLog(string title, string content)
        //{
        //    this.WriteErrorLog(title, content, null, false);
        //}

        /// <summary>
        /// 设置日志写入的对象
        /// </summary>
        /// <param name="writeLogObject"></param>
        private void SetWriteLogObject(Dictionary<string, string> writeLogObject)
        {
            if (!writeLogObject.ContainsKey("CurrentSeqCode"))
            {
                writeLogObject.Add("CurrentSeqCode", this.CurrentSeqCode);
            }
            if (!writeLogObject.ContainsKey("CmdId"))
            {
                writeLogObject.Add("CmdId", this.CmdId);
            }
            if (!writeLogObject.ContainsKey("CurrentTime"))
            {
                writeLogObject.Add("CurrentTime", DateTime.Now.ToString());
            }
        }

    }
}